package com.lzp.service.impl.center;

import com.github.pagehelper.PageHelper;
import com.lzp.mapper.*;
import com.lzp.pojo.OrderItems;
import com.lzp.pojo.OrderStatus;
import com.lzp.pojo.Orders;
import com.lzp.pojo.bo.center.OrderItemsCommentBO;
import com.lzp.pojo.vo.MyCommentVO;
import com.lzp.service.center.MyCommentsService;
import com.lzp.service.impl.BaseServiceImpl;
import com.lzp.utils.PagedGridResult;
import com.lzp.utils.enums.Yes0rNo;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户中心--评论实现
 *
 * @author dtyy
 * @date 2020/10/19
 */
@Service
public class MyCommentsServiceImpl extends BaseServiceImpl implements MyCommentsService {

    @Autowired
    public OrderItemsMapper orderItemsMapper;

    @Autowired
    private ItemsCommentsMapperCustom itemsCommentsMapperCustom;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderStatusMapper orderStatusMapper;

    @Autowired
    private Sid sid;

    /**
     * 根据订单id查询管理商品
     *
     * @param orderId 订单id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<OrderItems> queryPendingComment(String orderId) {
        OrderItems params = new OrderItems();
        params.setOrderId(orderId);
        return orderItemsMapper.select(params);
    }

    /**
     * 保存用户订单评论
     *
     * @param orderId     订单id
     * @param userId      用户id
     * @param commentList 评论列表
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveComments(String orderId,
                             String userId,
                             List<OrderItemsCommentBO> commentList) {
        // 1 保存数据到商品评价表
        // 为每个评论添加评论id
        for (OrderItemsCommentBO comment : commentList) {
            comment.setCommentId(sid.nextShort());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("commentList", commentList);
        itemsCommentsMapperCustom.saveComments(map);

        // 2 修改订单评价状态
        Orders order = new Orders();
        order.setId(orderId);
        order.setIsComment(Yes0rNo.YES.getCode());
        ordersMapper.updateByPrimaryKeySelective(order);

        // 3 修改订单状态表留言时间
        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderId(orderId);
        orderStatus.setCommentTime(new Date());
        orderStatusMapper.updateByPrimaryKeySelective(orderStatus);

        // 分开为了解耦
    }

    /**
     * 查询用户评价
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedGridResult queryMyComments(String userId,
                                           Integer page,
                                           Integer pageSize) {

        PageHelper.startPage(page, pageSize);
        List<MyCommentVO> list = itemsCommentsMapperCustom.queryMyComments(userId);

        return wrapPagedGrid(list, page);
    }

}
